﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.Models;
namespace UcAsp.Controllers
{
    public class AdminLoginController : Controller
    {
        //
        // GET: /AdminLogin/
        UcAspNetEntities entities = new UcAspNetEntities();
        public ActionResult Index()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            return View();
        }
        [HttpGet]
        public ActionResult Login()
        {

            return View();
        }
       
        public ActionResult UserLogin()
        {
            Message message = new Message { Result = false, Messages="用户或密码错误" };
            string username = Request.Form["UserName"];
            string password = Request.Form["UserPassword"];
            AdminUser user = entities.AdminUsers.FirstOrDefault(o=>(o.UserName==username&&o.UserPassword==password));
            if (user != null)
            {
                Session["Info"] = "Admin";
                Session["AdminID"] = user.ID;
                Session["UserName"] = user.UserName;
                user.LastLoginTime = user.LoginTime;
                user.LoginTime = DateTime.Now;
                entities.SaveChanges();
                message.Result = true;
            }
            
            return Json(message, JsonRequestBehavior.AllowGet);
        }
        public ActionResult LoginOut()
        {
            Session.Clear();

            return Redirect("/");
        }
        public ActionResult ShowAdmin()
        { 
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            int id=int.Parse(Session["AdminID"].ToString());
            AdminUser admin = entities.AdminUsers.FirstOrDefault(o => o.ID == id);
            return View(admin);
        }
        public ActionResult SaveAdmin()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            int id = int.Parse(Session["AdminID"].ToString());
            AdminUser admin = entities.AdminUsers.FirstOrDefault(o => o.ID == id);
            string password = Request.Form["Password"];
            admin.UserPassword = password;
            entities.SaveChanges();
            Message message = new Message { Result = true };
            return Json(message, JsonRequestBehavior.AllowGet);
        }

    }
}
